home *** CD-ROM | disk | FTP | other *** search
/ Cream of the Crop 26 / Cream of the Crop 26.iso / os2 / plnk081.zip / pilot-link.0.8.1 / pilot-file.c < prev    next >
C/C++ Source or Header  |  1997-08-03  |  7KB  |  322 lines

  1. /*
  2.  * Pilot File dump utility
  3.  * Pace Willisson <pace@blitz.com> December 1996
  4.  * Additions by Kenneth Albanowski
  5.  *
  6.  * This is free software, licensed under the GNU Public License V2.
  7.  * See the file COPYING for details.
  8.  */
  9.           
  10. #include <stdio.h>
  11. #include <stdlib.h>
  12. #include <unistd.h>
  13. #include <time.h>
  14.  
  15. #include "pi-source.h"
  16. #include "pi-socket.h"
  17. #include "pi-dlp.h"
  18. #include "pi-file.h"
  19.  
  20. #ifdef sun
  21.   extern char* optarg;
  22.   extern int optind;
  23. #endif
  24.  
  25. void dump_header (struct pi_file *pf, struct DBInfo *ip);
  26. void dump_app_info (struct pi_file *pf, struct DBInfo *ip);
  27. void dump_sort_info (struct pi_file *pf, struct DBInfo *ip);
  28. void list_records (struct pi_file *pf, struct DBInfo *ip);
  29. void dump_record (struct pi_file *pf, struct DBInfo *ip, int record);
  30.  
  31. char *iso_time_str (time_t t);
  32. void dump (void *buf, int size);
  33.  
  34. char * progname;
  35.  
  36. void
  37. usage (void)
  38. {
  39.   fprintf (stderr, "usage: %s [options] file\n", progname);
  40.   fprintf (stderr, "  -h      dump header\n");
  41.   fprintf (stderr, "  -a      dump app_info\n");
  42.   fprintf (stderr, "  -s      dump sort_info\n");
  43.   fprintf (stderr, "  -l      list records\n");
  44.   fprintf (stderr, "  -r #    dump a record\n");
  45.   fprintf (stderr, "  -v      dump all data and all records\n");
  46.   fprintf (stderr, "  -R, -V  as -r, -v, but also dump resources to files\n");
  47.   exit (1);
  48. }
  49.  
  50. int hflag=0, aflag=0, sflag=0, vflag=0, lflag=0, rflag=0, rnum=0, filedump=0;
  51.  
  52. int
  53. main (int argc, char **argv)
  54. {
  55.   struct pi_file *pf;
  56.   int c;
  57.   char *name;
  58.   struct DBInfo info;
  59.   
  60.   progname = argv[0];
  61.  
  62.   while ((c = getopt (argc, argv, "haslr:R:vV")) != EOF) {
  63.     switch (c) {
  64.     case 'h':
  65.       hflag = 1;
  66.       break;
  67.     case 'a':
  68.       aflag = 1;
  69.       break;
  70.     case 's':
  71.       sflag = 1;
  72.       break;
  73.     case 'V':
  74.       filedump = 1;
  75.       /* FALLTHROUGH */
  76.     case 'v':
  77.       vflag = 1;
  78.       break;
  79.     case 'l':
  80.       lflag = 1;
  81.       break;
  82.     case 'R':
  83.       filedump = 1;
  84.       /* FALLTHROUGH */
  85.     case 'r':
  86.       rflag = 1;
  87.       rnum = atoi(optarg);
  88.       break;
  89.     default:
  90.       usage ();
  91.     }
  92.   }
  93.  
  94.   if (optind >= argc)
  95.     usage ();
  96.   
  97.   name = argv[optind++];
  98.  
  99.   if (optind != argc)
  100.     usage ();
  101.  
  102.   if ((pf = pi_file_open (name)) == NULL) {
  103.     fprintf (stderr, "can't open %s\n", name);
  104.     exit (1);
  105.   }
  106.  
  107.   if (pi_file_get_info (pf, &info) < 0) {
  108.     fprintf (stderr, "can't get info\n\n");
  109.     exit (1);
  110.   }
  111.   
  112.   if (hflag || vflag)
  113.     dump_header (pf, &info);
  114.  
  115.   if (aflag || vflag)
  116.     dump_app_info (pf, &info);
  117.  
  118.   if (sflag || vflag)
  119.     dump_sort_info (pf, &info);
  120.  
  121.   if (lflag || vflag)
  122.     list_records(pf, &info);
  123.   
  124.   if (rflag)
  125.     dump_record(pf, &info, rnum);
  126.     
  127.   return (0);
  128. }
  129.  
  130. char *
  131. iso_time_str (time_t t)
  132. {
  133.   struct tm tm;
  134.   static char buf[50];
  135.  
  136.   tm = *localtime (&t);
  137.   sprintf (buf, "%04d-%02d-%02d %02d:%02d:%02d",
  138.        tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday,
  139.        tm.tm_hour, tm.tm_min, tm.tm_sec);
  140.   return (buf);
  141. }
  142.  
  143. void
  144. dump (void *buf, int n)
  145. {
  146.   int i, j, c;
  147.  
  148.   for (i = 0; i < n; i += 16) {
  149.     printf ("%04x: ", i);
  150.     for (j = 0; j < 16; j++) {
  151.       if (i+j < n)
  152.     printf ("%02x ", ((unsigned char *)buf)[i+j]);
  153.       else
  154.     printf ("   ");
  155.     }
  156.     printf ("  ");
  157.     for (j = 0; j < 16 && i+j < n; j++) {
  158.       c = ((unsigned char *)buf)[i+j] & 0x7f;
  159.       if (c < ' ' || c >= 0x7f)
  160.     putchar ('.');
  161.       else
  162.     putchar (c);
  163.     }
  164.     printf ("\n");
  165.   }
  166. }
  167.  
  168. void
  169. dump_header (struct pi_file *pf, struct DBInfo *ip)
  170. {
  171.   printf ("name: \"%s\"\n", ip->name);
  172.   printf ("flags: 0x%x", ip->flags);
  173.   if (ip->flags & dlpDBFlagNewer) printf (" NEWER");
  174.   if (ip->flags & dlpDBFlagReset) printf (" RESET");
  175.   if (ip->flags & dlpDBFlagResource) printf (" RESOURCE");
  176.   if (ip->flags & dlpDBFlagReadOnly) printf (" READ_ONLY");
  177.   if (ip->flags & dlpDBFlagAppInfoDirty) printf (" APP-INFO-DIRTY");
  178.   if (ip->flags & dlpDBFlagBackup) printf (" BACKUP");
  179.   if (ip->flags & dlpDBFlagOpen) printf (" OPEN");
  180.   printf ("\n");
  181.   printf ("version: %d\n", ip->version);
  182.   printf ("creation_time: %s\n", iso_time_str (ip->createDate));
  183.   printf ("modified_time: %s\n", iso_time_str (ip->modifyDate));
  184.   printf ("backup_time: %s\n", iso_time_str (ip->backupDate));
  185.   printf ("modification_number: %ld\n", ip->modnum);
  186.   printf ("type: '%s', ", printlong(ip->type));
  187.   printf ("creator: '%s'\n", printlong(ip->creator));
  188.   printf ("\n");
  189. }
  190.  
  191. void
  192. dump_app_info (struct pi_file *pf, struct DBInfo *ip)
  193. {
  194.   void *app_info;
  195.   int app_info_size;
  196.  
  197.   if (pi_file_get_app_info (pf, &app_info, &app_info_size) < 0) {
  198.     printf ("can't get app_info\n\n");
  199.     return;
  200.   }
  201.  
  202.   printf ("app_info_size %d\n", app_info_size);
  203.   dump (app_info, app_info_size);
  204.   printf ("\n");
  205. }
  206.  
  207. void
  208. dump_sort_info (struct pi_file *pf, struct DBInfo *ip)
  209. {
  210.   void *sort_info;
  211.   int sort_info_size;
  212.  
  213.   if (pi_file_get_sort_info (pf, &sort_info, &sort_info_size) < 0) {
  214.     printf ("can't get sort_info\n\n");
  215.     return;
  216.   }
  217.  
  218.   printf ("sort_info_size %d\n", sort_info_size);
  219.   dump (sort_info, sort_info_size);
  220.   printf ("\n");
  221. }
  222.  
  223. void
  224. list_records (struct pi_file *pf, struct DBInfo *ip)
  225. {
  226.   int entnum;
  227.   int size;
  228.   unsigned long type, uid;
  229.   int id;
  230.   void *buf;
  231.   int nentries;
  232.   int attrs, cat;
  233.  
  234.  
  235.   pi_file_get_entries (pf, &nentries);
  236.  
  237.   if (ip->flags & dlpDBFlagResource) {
  238.     printf ("entries\n");
  239.     printf ("index\tsize\ttype\tid\n");
  240.     for (entnum = 0; entnum < nentries; entnum++) {
  241.       if (pi_file_read_resource (pf, entnum, &buf, &size, &type, &id) < 0) {
  242.         printf ("error reading %d\n\n", entnum);
  243.         return;
  244.       }
  245.       printf ("%d\t%d\t%s\t%d\n", entnum, size, printlong(type), id);
  246.       if (vflag) {
  247.         dump(buf,size);
  248.         printf ("\n");
  249.     if (filedump) {
  250.         FILE *fp;
  251.         char name[64];
  252.         sprintf(name, "%4s%04x.bin", printlong(type), id);
  253.         fp = fopen(name, "w");
  254.         fwrite(buf, size, 1, fp);
  255.         fclose(fp);
  256.         printf("(written to %s)\n", name);
  257.     }
  258.       }
  259.     }
  260.   } else {
  261.     printf ("entries\n");
  262.     printf ("index\tsize\tattrs\tcat\tuid\n");
  263.     for (entnum = 0; entnum < nentries; entnum++) {
  264.       if (pi_file_read_record (pf, entnum, &buf, &size,
  265.                    &attrs, &cat, &uid) < 0) {
  266.         printf ("error reading %d\n\n", entnum);
  267.         return;
  268.       }
  269.       printf ("%d\t%d\t0x%x\t%d\t0x%lx\n", entnum, size, attrs, cat, uid);
  270.       if (vflag) {
  271.         dump(buf,size);
  272.         printf ("\n");
  273.       }
  274.     }
  275.   }
  276.  
  277.   printf ("\n");
  278. }
  279.  
  280. void
  281. dump_record (struct pi_file *pf, struct DBInfo *ip, int record)
  282. {
  283.   int size;
  284.   unsigned long type, uid;
  285.   int id;
  286.   void *buf;
  287.   int attrs, cat;
  288.  
  289.   if (ip->flags & dlpDBFlagResource) {
  290.     printf ("entries\n");
  291.     printf ("index\tsize\ttype\tid\n");
  292.     if (pi_file_read_resource (pf, record, &buf, &size, &type, &id) < 0) {
  293.       printf ("error reading resource #%d\n\n", record);
  294.       return;
  295.     }
  296.     printf ("%d\t%d\t%s\t%d\n", record, size, printlong(type), id);
  297.     dump(buf,size);
  298.     if (filedump) {
  299.     FILE *fp;
  300.     char name[64];
  301.     sprintf(name, "%4s%04x.bin", printlong(type), id);
  302.     fp = fopen(name, "w");
  303.     fwrite(buf, size, 1, fp);
  304.     fclose(fp);
  305.     printf("(written to %s)\n", name);
  306.     }
  307.   } else {
  308.     printf ("entries\n");
  309.     printf ("index\tsize\tattrs\tcat\tuid\n");
  310.     if (pi_file_read_record (pf, record, &buf, &size,
  311.                  &attrs, &cat, &uid) < 0) {
  312.       printf ("error reading record #%d\n\n", record);
  313.       return;
  314.     }
  315.     printf ("%d\t%d\t0x%x\t%d\t0x%lx\n", record, size, attrs, cat, uid);
  316.     dump(buf,size);
  317.   }
  318.  
  319.   printf ("\n");
  320. }
  321.  
  322.